package com.wowocai.btchina.jobs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.wowocai.btchina.service.SearchEngineService;

@Component
@EnableScheduling
public class AllInOneJobs {

    private final static Logger logger = LoggerFactory.getLogger(AllInOneJobs.class);

    @Autowired
    private SearchEngineService searchEngineService;

    @Value("${spring.schedule.runable}")
    private boolean runable = true;

    /**
     * 每6小时将新数据同步到ES当中
     */
    @Scheduled(cron = "0 5 0/6 * * ?")
    public void pushMongoToElasticsearchJob() {
        if (!runable) {
            return;
        }
        logger.info("..........start pushMongoToElasticsearch");
        searchEngineService.pushMongoToElasticsearch();
        logger.info("..........end pushMongoToElasticsearch");

    }

    /**
     * 每天1点，将前一天有更新的数据同步到ES
     */
    @Scheduled(cron = "0 30 0 * * ?")
    public void syncVisitCountToEsByDateJob() {
        if (!runable) {
            return;
        }
        logger.info("..........start syncVisitCountToEsByDateJob");
        searchEngineService.syncVisitCountToEsByDate();
        logger.info("..........end syncVisitCountToEsByDateJob");

    }

    /**
     * 1点生成网站地图
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void createSiteMapJob() {
        if (!runable) {
            return;
        }
        logger.info("..........start createSiteMapJob");
        searchEngineService.createSiteMap();
        logger.info("..........end createSiteMapJob");

    }

    /**
     * ping google
     */
    @Scheduled(cron = "0 30 1 * * ?")
    public void pingGoogleJob() {
        if (!runable) {
            return;
        }
        logger.info("..........start pingGoogleJob");
        searchEngineService.pingGoogle();
        logger.info("..........end pingGoogleJob");

    }

}
